#!/usr/bin/python
#-*- coding:utf-8 -*-
'''
@func:
删除指定目录及其子目录下的.svn文件夹;
注意不适用于'XXX.svnYYY'，XXX和YYY为字母数字的情况。
@author: chaos
@date: 2010 11 30 16:46
'''
import os
import sys

if __name__ == '__main__':
    path = ''
    if len(sys.argv) < 2:
        path = '.'
    else:
        path = sys.argv[1]
    # 获得指定目录下所有.svn目录
    svn_dir = []
    for root, dirs, files in os.walk(path, topdown = False):
        for name in dirs:
            if name.endswith('.svn'):
                svn_dir.append(os.path.join(root, name))
    
    if len(svn_dir) == 0:
        print 'Nothing to Del!'
        sys.exit(0)
    
    i = 0
    for name in svn_dir:
        print name
        i = i + 1
    print 'DIRs will be deleted!!'
    input = raw_input('Are you SURE(Y/N)?')
    if input == 'N' or input == 'n':
        sys.exit(0)
    
    # 删除.svn子目录
    for root, dirs, files in os.walk(path, topdown = False):
        for name in files:
            if root.find('.svn') != -1:
                os.remove(os.path.join(root, name))
            
        for name in dirs:
            # 删除父目录含有.svn的文件夹
            if os.name == 'nt':
                if root.find('\\.svn') != -1:
                    print 'file:' + os.path.join(root, name)
                    os.rmdir(os.path.join(root, name))
                elif name == '.svn':
                    print 'dir:' + os.path.join(root, name)
                    os.rmdir(os.path.join(root, name))
            else:
                if root.find('/.svn') != -1:
                    print 'file:' + os.path.join(root, name)
                    os.rmdir(os.path.join(root, name))
                elif name == '.svn':
                    print 'dir:' + os.path.join(root, name)
                    os.rmdir(os.path.join(root, name))


    